from mod_python import apache

import MySQLdb as mdb
import sys, os
import ConfigParser
import urllib2

def handler(req):
    req.content_type = 'text/html'
    try:
	#get url and partner from request

        config = ConfigParser.ConfigParser()
        __here__ = os.path.dirname(__file__)
        config.readfp(open(os.path.join(__here__,"app.cfg")))
        
	#fetch data from config file

        instance = config.get("mysqld","instance")
        user = config.get("mysqld","user")
        password = config.get("mysqld","password")
        database = config.get("mysqld","database")
	
	#fetch PartnerID and MD5 trackingURL
		       
	url = req.uri
	params = url.split('/')
	url = params[len(params) - 1]
	partnerID = params[len(params) - 2]

	#db access	
	conn = mdb.connect(instance,user,password,database)
	cursor = conn.cursor()
	
	if partnerID not in ('totals'):         
		cursor.execute("call sp_PartnerURLLookup('%s',%s)" % (url,partnerID))
	      	data = cursor.fetchone()
        
        cursor.close()
        conn.close()

    except mdb.Error, e:
           req.write("Error %d: %s" % (e.args[0],e.args[1]))    
        	
    #req.write(data)
    return apache.OK

